package edu.yale.its.tp.email.conversion.util;

/**
 * <pre>
 * Copyright (c) 2000-2003 Yale University. All rights reserved.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS," AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE EXPRESSLY
 * DISCLAIMED. IN NO EVENT SHALL YALE UNIVERSITY OR ITS EMPLOYEES BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED, THE COSTS OF
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * 
 * Redistribution and use of this software in source or binary forms,
 * with or without modification, are permitted, provided that the
 * following conditions are met:
 * 
 * 1. Any redistribution must include the above copyright notice and
 * disclaimer and this list of conditions in any related documentation
 * and, if feasible, in the redistributed software.
 * 
 * 2. Any redistribution must include the acknowledgment, "This product
 * includes software developed by Yale University," in any related
 * documentation and, if feasible, in the redistributed software.
 * 
 * 3. The names "Yale" and "Yale University" must not be used to endorse
 * or promote products derived from this software.
 * </pre>
 *
 */

public enum ByteFormatter {
    BYTE     ( "B", 1L),
    KILOBYTE ("KB", 1L << 10),
    MEGABYTE ("MB", 1L << 20),
    GIGABYTE ("GB", 1L << 30),
    TERABYTE ("TB", 1L << 40),
    PETABYTE ("PB", 1L << 50),
    EXABYTE  ("EB", 1L << 60);

    public static final ByteFormatter BASE = BYTE;

    private static java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
	static {
		nf.setGroupingUsed(false);
		nf.setMinimumFractionDigits(0);
		nf.setMaximumFractionDigits(1);
	}

    private final String symbol;
    private final long divider;  // divider of BASE unit

    ByteFormatter(String name, long divider) {
        this.symbol = name;
        this.divider = divider;
    }

    public static ByteFormatter of(final long number) {
        final long n = number > 0 ? -number : number;
        if (n > -(1L << 10)) {
            return BYTE;
        } else if (n > -(1L << 20)) {
            return KILOBYTE;
        } else if (n > -(1L << 30)) {
            return MEGABYTE;
        } else if (n > -(1L << 40)) {
            return GIGABYTE;
        } else if (n > -(1L << 50)) {
            return TERABYTE;
        } else if (n > -(1L << 60)) {
            return PETABYTE;
        } else {  // n >= Long.MIN_VALUE
            return EXABYTE;
        }
    }

    public String format(long number) {
        return nf.format((double)number / divider) + " " + symbol;
    }

} 
